﻿#region Using directives

using System;
using System.Data;
using System.Data.Common;
using System.Collections;
using System.Collections.Generic;

using GPSTrackingDBV2.Entities;
using GPSTrackingDBV2.Data;

#endregion

namespace GPSTrackingDBV2.Data.Bases
{	
	///<summary>
	/// This class is the base class for any <see cref="MastersProviderBase"/> implementation.
	/// It exposes CRUD methods as well as selecting on index, foreign keys and custom stored procedures.
	///</summary>
	public abstract partial class MastersProviderBaseCore : EntityProviderBase<GPSTrackingDBV2.Entities.Masters, GPSTrackingDBV2.Entities.MastersKey>
	{		
		#region Get from Many To Many Relationship Functions
		#region GetByCompanyIdFromCompaniesByMasters
		
		/// <summary>
		///		Gets Masters objects from the datasource by CompanyID in the
		///		CompaniesByMasters table. Table Masters is related to table Companies
		///		through the (M:N) relationship defined in the CompaniesByMasters table.
		/// </summary>
		/// <param name="_companyId"></param>
		/// <returns>Returns a typed collection of Masters objects.</returns>
		public TList<Masters> GetByCompanyIdFromCompaniesByMasters(System.Int32 _companyId)
		{
			int count = -1;
			return GetByCompanyIdFromCompaniesByMasters(null,_companyId, 0, int.MaxValue, out count);
			
		}
		
		/// <summary>
		///		Gets GPSTrackingDBV2.Entities.Masters objects from the datasource by CompanyID in the
		///		CompaniesByMasters table. Table Masters is related to table Companies
		///		through the (M:N) relationship defined in the CompaniesByMasters table.
		/// </summary>
		/// <param name="start">Row number at which to start reading, the first row is 0.</param>
		/// <param name="pageLength">Number of rows to return.</param>
		/// <param name="_companyId"></param>
		/// <remarks></remarks>
		/// <returns>Returns a TList of Masters objects.</returns>
		public TList<Masters> GetByCompanyIdFromCompaniesByMasters(System.Int32 _companyId, int start, int pageLength)
		{
			int count = -1;
			return GetByCompanyIdFromCompaniesByMasters(null, _companyId, start, pageLength, out count);
		}
		
		/// <summary>
		///		Gets Masters objects from the datasource by CompanyID in the
		///		CompaniesByMasters table. Table Masters is related to table Companies
		///		through the (M:N) relationship defined in the CompaniesByMasters table.
		/// </summary>
		/// <param name="transactionManager"><see cref="TransactionManager"/> object</param>
		/// <param name="_companyId"></param>
		/// <remarks></remarks>
		/// <returns>Returns a typed collection of Masters objects.</returns>
		public TList<Masters> GetByCompanyIdFromCompaniesByMasters(TransactionManager transactionManager, System.Int32 _companyId)
		{
			int count = -1;
			return GetByCompanyIdFromCompaniesByMasters(transactionManager, _companyId, 0, int.MaxValue, out count);
		}
		
		
		/// <summary>
		///		Gets Masters objects from the datasource by CompanyID in the
		///		CompaniesByMasters table. Table Masters is related to table Companies
		///		through the (M:N) relationship defined in the CompaniesByMasters table.
		/// </summary>
		/// <param name="transactionManager"><see cref="TransactionManager"/> object</param>
		/// <param name="_companyId"></param>
		/// <param name="start">Row number at which to start reading, the first row is 0.</param>
		///  <param name="pageLength">Number of rows to return.</param>
		/// <remarks></remarks>
		/// <returns>Returns a typed collection of Masters objects.</returns>
		public TList<Masters> GetByCompanyIdFromCompaniesByMasters(TransactionManager transactionManager, System.Int32 _companyId,int start, int pageLength)
		{
			int count = -1;
			return GetByCompanyIdFromCompaniesByMasters(transactionManager, _companyId, start, pageLength, out count);
		}
		
		/// <summary>
		///		Gets Masters objects from the datasource by CompanyID in the
		///		CompaniesByMasters table. Table Masters is related to table Companies
		///		through the (M:N) relationship defined in the CompaniesByMasters table.
		/// </summary>
		/// <param name="_companyId"></param>
		/// <param name="start">Row number at which to start reading, the first row is 0.</param>
		///  <param name="pageLength">Number of rows to return.</param>
		/// <param name="count">out parameter to get total records for query</param>
		/// <remarks></remarks>
		/// <returns>Returns a typed collection of Masters objects.</returns>
		public TList<Masters> GetByCompanyIdFromCompaniesByMasters(System.Int32 _companyId,int start, int pageLength, out int count)
		{
			
			return GetByCompanyIdFromCompaniesByMasters(null, _companyId, start, pageLength, out count);
		}


		/// <summary>
		///		Gets Masters objects from the datasource by CompanyID in the
		///		CompaniesByMasters table. Table Masters is related to table Companies
		///		through the (M:N) relationship defined in the CompaniesByMasters table.
		/// </summary>
		/// <param name="transactionManager"><see cref="TransactionManager"/> object</param>
		/// <param name="start">Row number at which to start reading, the first row is 0.</param>
		/// <param name="pageLength">Number of rows to return.</param>
		/// <param name="count">out parameter to get total records for query</param>
		/// <param name="_companyId"></param>
		/// <remarks></remarks>
		/// <returns>Returns a TList of Masters objects.</returns>
		public abstract TList<Masters> GetByCompanyIdFromCompaniesByMasters(TransactionManager transactionManager,System.Int32 _companyId, int start, int pageLength, out int count);
		
		#endregion GetByCompanyIdFromCompaniesByMasters
		
		#region GetByMasterRoleIdFromMastersRolesByMaster
		
		/// <summary>
		///		Gets Masters objects from the datasource by MasterRoleID in the
		///		MastersRolesByMaster table. Table Masters is related to table MasterRoles
		///		through the (M:N) relationship defined in the MastersRolesByMaster table.
		/// </summary>
		/// <param name="_masterRoleId"></param>
		/// <returns>Returns a typed collection of Masters objects.</returns>
		public TList<Masters> GetByMasterRoleIdFromMastersRolesByMaster(System.Int32 _masterRoleId)
		{
			int count = -1;
			return GetByMasterRoleIdFromMastersRolesByMaster(null,_masterRoleId, 0, int.MaxValue, out count);
			
		}
		
		/// <summary>
		///		Gets GPSTrackingDBV2.Entities.Masters objects from the datasource by MasterRoleID in the
		///		MastersRolesByMaster table. Table Masters is related to table MasterRoles
		///		through the (M:N) relationship defined in the MastersRolesByMaster table.
		/// </summary>
		/// <param name="start">Row number at which to start reading, the first row is 0.</param>
		/// <param name="pageLength">Number of rows to return.</param>
		/// <param name="_masterRoleId"></param>
		/// <remarks></remarks>
		/// <returns>Returns a TList of Masters objects.</returns>
		public TList<Masters> GetByMasterRoleIdFromMastersRolesByMaster(System.Int32 _masterRoleId, int start, int pageLength)
		{
			int count = -1;
			return GetByMasterRoleIdFromMastersRolesByMaster(null, _masterRoleId, start, pageLength, out count);
		}
		
		/// <summary>
		///		Gets Masters objects from the datasource by MasterRoleID in the
		///		MastersRolesByMaster table. Table Masters is related to table MasterRoles
		///		through the (M:N) relationship defined in the MastersRolesByMaster table.
		/// </summary>
		/// <param name="transactionManager"><see cref="TransactionManager"/> object</param>
		/// <param name="_masterRoleId"></param>
		/// <remarks></remarks>
		/// <returns>Returns a typed collection of Masters objects.</returns>
		public TList<Masters> GetByMasterRoleIdFromMastersRolesByMaster(TransactionManager transactionManager, System.Int32 _masterRoleId)
		{
			int count = -1;
			return GetByMasterRoleIdFromMastersRolesByMaster(transactionManager, _masterRoleId, 0, int.MaxValue, out count);
		}
		
		
		/// <summary>
		///		Gets Masters objects from the datasource by MasterRoleID in the
		///		MastersRolesByMaster table. Table Masters is related to table MasterRoles
		///		through the (M:N) relationship defined in the MastersRolesByMaster table.
		/// </summary>
		/// <param name="transactionManager"><see cref="TransactionManager"/> object</param>
		/// <param name="_masterRoleId"></param>
		/// <param name="start">Row number at which to start reading, the first row is 0.</param>
		///  <param name="pageLength">Number of rows to return.</param>
		/// <remarks></remarks>
		/// <returns>Returns a typed collection of Masters objects.</returns>
		public TList<Masters> GetByMasterRoleIdFromMastersRolesByMaster(TransactionManager transactionManager, System.Int32 _masterRoleId,int start, int pageLength)
		{
			int count = -1;
			return GetByMasterRoleIdFromMastersRolesByMaster(transactionManager, _masterRoleId, start, pageLength, out count);
		}
		
		/// <summary>
		///		Gets Masters objects from the datasource by MasterRoleID in the
		///		MastersRolesByMaster table. Table Masters is related to table MasterRoles
		///		through the (M:N) relationship defined in the MastersRolesByMaster table.
		/// </summary>
		/// <param name="_masterRoleId"></param>
		/// <param name="start">Row number at which to start reading, the first row is 0.</param>
		///  <param name="pageLength">Number of rows to return.</param>
		/// <param name="count">out parameter to get total records for query</param>
		/// <remarks></remarks>
		/// <returns>Returns a typed collection of Masters objects.</returns>
		public TList<Masters> GetByMasterRoleIdFromMastersRolesByMaster(System.Int32 _masterRoleId,int start, int pageLength, out int count)
		{
			
			return GetByMasterRoleIdFromMastersRolesByMaster(null, _masterRoleId, start, pageLength, out count);
		}


		/// <summary>
		///		Gets Masters objects from the datasource by MasterRoleID in the
		///		MastersRolesByMaster table. Table Masters is related to table MasterRoles
		///		through the (M:N) relationship defined in the MastersRolesByMaster table.
		/// </summary>
		/// <param name="transactionManager"><see cref="TransactionManager"/> object</param>
		/// <param name="start">Row number at which to start reading, the first row is 0.</param>
		/// <param name="pageLength">Number of rows to return.</param>
		/// <param name="count">out parameter to get total records for query</param>
		/// <param name="_masterRoleId"></param>
		/// <remarks></remarks>
		/// <returns>Returns a TList of Masters objects.</returns>
		public abstract TList<Masters> GetByMasterRoleIdFromMastersRolesByMaster(TransactionManager transactionManager,System.Int32 _masterRoleId, int start, int pageLength, out int count);
		
		#endregion GetByMasterRoleIdFromMastersRolesByMaster
		
		#endregion	
		
		#region Delete Methods

		/// <summary>
		/// 	Deletes a row from the DataSource.
		/// </summary>
		/// <param name="transactionManager">A <see cref="TransactionManager"/> object.</param>
		/// <param name="key">The unique identifier of the row to delete.</param>
		/// <returns>Returns true if operation suceeded.</returns>
		public override bool Delete(TransactionManager transactionManager, GPSTrackingDBV2.Entities.MastersKey key)
		{
			return Delete(transactionManager, key.MasterUsername);
		}
		
		/// <summary>
		/// 	Deletes a row from the DataSource.
		/// </summary>
		/// <param name="_masterUsername">. Primary Key.</param>
		/// <remarks>Deletes based on primary key(s).</remarks>
		/// <returns>Returns true if operation suceeded.</returns>
		public bool Delete(System.String _masterUsername)
		{
			return Delete(null, _masterUsername);
		}
		
		/// <summary>
		/// 	Deletes a row from the DataSource.
		/// </summary>
		/// <param name="transactionManager"><see cref="TransactionManager"/> object</param>
		/// <param name="_masterUsername">. Primary Key.</param>
		/// <remarks>Deletes based on primary key(s).</remarks>
		/// <returns>Returns true if operation suceeded.</returns>
		public abstract bool Delete(TransactionManager transactionManager, System.String _masterUsername);		
		
		#endregion Delete Methods
		
		#region Get By Foreign Key Functions
		#endregion

		#region Get By Index Functions
		
		/// <summary>
		/// 	Gets a row from the DataSource based on its primary key.
		/// </summary>
		/// <param name="transactionManager">A <see cref="TransactionManager"/> object.</param>
		/// <param name="key">The unique identifier of the row to retrieve.</param>
		/// <param name="start">Row number at which to start reading, the first row is 0.</param>
		/// <param name="pageLength">Number of rows to return.</param>
		/// <returns>Returns an instance of the Entity class.</returns>
		public override GPSTrackingDBV2.Entities.Masters Get(TransactionManager transactionManager, GPSTrackingDBV2.Entities.MastersKey key, int start, int pageLength)
		{
			return GetByMasterUsername(transactionManager, key.MasterUsername, start, pageLength);
		}
		
		/// <summary>
		/// 	Gets rows from the datasource based on the primary key PK_Masters index.
		/// </summary>
		/// <param name="_masterUsername"></param>
		/// <returns>Returns an instance of the <see cref="GPSTrackingDBV2.Entities.Masters"/> class.</returns>
		public GPSTrackingDBV2.Entities.Masters GetByMasterUsername(System.String _masterUsername)
		{
			int count = -1;
			return GetByMasterUsername(null,_masterUsername, 0, int.MaxValue, out count);
		}
		
		/// <summary>
		/// 	Gets rows from the datasource based on the PK_Masters index.
		/// </summary>
		/// <param name="_masterUsername"></param>
		/// <param name="start">Row number at which to start reading, the first row is 0.</param>
		/// <param name="pageLength">Number of rows to return.</param>
		/// <remarks></remarks>
		/// <returns>Returns an instance of the <see cref="GPSTrackingDBV2.Entities.Masters"/> class.</returns>
		public GPSTrackingDBV2.Entities.Masters GetByMasterUsername(System.String _masterUsername, int start, int pageLength)
		{
			int count = -1;
			return GetByMasterUsername(null, _masterUsername, start, pageLength, out count);
		}
		
		/// <summary>
		/// 	Gets rows from the datasource based on the PK_Masters index.
		/// </summary>
		/// <param name="transactionManager"><see cref="TransactionManager"/> object</param>
		/// <param name="_masterUsername"></param>
		/// <remarks></remarks>
		/// <returns>Returns an instance of the <see cref="GPSTrackingDBV2.Entities.Masters"/> class.</returns>
		public GPSTrackingDBV2.Entities.Masters GetByMasterUsername(TransactionManager transactionManager, System.String _masterUsername)
		{
			int count = -1;
			return GetByMasterUsername(transactionManager, _masterUsername, 0, int.MaxValue, out count);
		}
		
		/// <summary>
		/// 	Gets rows from the datasource based on the PK_Masters index.
		/// </summary>
		/// <param name="transactionManager"><see cref="TransactionManager"/> object</param>
		/// <param name="_masterUsername"></param>
		/// <param name="start">Row number at which to start reading, the first row is 0.</param>
		/// <param name="pageLength">Number of rows to return.</param>
		/// <remarks></remarks>
		/// <returns>Returns an instance of the <see cref="GPSTrackingDBV2.Entities.Masters"/> class.</returns>
		public GPSTrackingDBV2.Entities.Masters GetByMasterUsername(TransactionManager transactionManager, System.String _masterUsername, int start, int pageLength)
		{
			int count = -1;
			return GetByMasterUsername(transactionManager, _masterUsername, start, pageLength, out count);
		}
		
		/// <summary>
		/// 	Gets rows from the datasource based on the PK_Masters index.
		/// </summary>
		/// <param name="_masterUsername"></param>
		/// <param name="start">Row number at which to start reading, the first row is 0.</param>
		/// <param name="pageLength">Number of rows to return.</param>
		/// <param name="count">out parameter to get total records for query</param>
		/// <remarks></remarks>
		/// <returns>Returns an instance of the <see cref="GPSTrackingDBV2.Entities.Masters"/> class.</returns>
		public GPSTrackingDBV2.Entities.Masters GetByMasterUsername(System.String _masterUsername, int start, int pageLength, out int count)
		{
			return GetByMasterUsername(null, _masterUsername, start, pageLength, out count);
		}
		
				
		/// <summary>
		/// 	Gets rows from the datasource based on the PK_Masters index.
		/// </summary>
		/// <param name="transactionManager"><see cref="TransactionManager"/> object</param>
		/// <param name="_masterUsername"></param>
		/// <param name="start">Row number at which to start reading, the first row is 0.</param>
		/// <param name="pageLength">Number of rows to return.</param>
		/// <param name="count">The total number of records.</param>
		/// <returns>Returns an instance of the <see cref="GPSTrackingDBV2.Entities.Masters"/> class.</returns>
		public abstract GPSTrackingDBV2.Entities.Masters GetByMasterUsername(TransactionManager transactionManager, System.String _masterUsername, int start, int pageLength, out int count);
						
		#endregion "Get By Index Functions"
	
		#region Custom Methods
		
		
		#endregion

		#region Helper Functions	
		
		/// <summary>
		/// Fill a TList&lt;Masters&gt; From a DataReader.
		/// </summary>
		/// <param name="reader">Datareader</param>
		/// <param name="rows">The collection to fill</param>
		/// <param name="start">Row number at which to start reading, the first row is 0.</param>
		/// <param name="pageLength">number of rows.</param>
		/// <returns>a <see cref="TList&lt;Masters&gt;"/></returns>
		public static TList<Masters> Fill(IDataReader reader, TList<Masters> rows, int start, int pageLength)
		{
			NetTiersProvider currentProvider = DataRepository.Provider;
            bool useEntityFactory = currentProvider.UseEntityFactory;
            bool enableEntityTracking = currentProvider.EnableEntityTracking;
            LoadPolicy currentLoadPolicy = currentProvider.CurrentLoadPolicy;
			Type entityCreationFactoryType = currentProvider.EntityCreationalFactoryType;
			
			// advance to the starting row
			for (int i = 0; i < start; i++)
			{
				if (!reader.Read())
				return rows; // not enough rows, just return
			}
			for (int i = 0; i < pageLength; i++)
			{
				if (!reader.Read())
					break; // we are done
					
				string key = null;
				
				GPSTrackingDBV2.Entities.Masters c = null;
				if (useEntityFactory)
				{
					key = new System.Text.StringBuilder("Masters")
					.Append("|").Append((System.String)reader[((int)MastersColumn.MasterUsername - 1)]).ToString();
					c = EntityManager.LocateOrCreate<Masters>(
					key.ToString(), // EntityTrackingKey
					"Masters",  //Creational Type
					entityCreationFactoryType,  //Factory used to create entity
					enableEntityTracking); // Track this entity?
				}
				else
				{
					c = new GPSTrackingDBV2.Entities.Masters();
				}
				
				if (!enableEntityTracking ||
					c.EntityState == EntityState.Added ||
					(enableEntityTracking &&
					
						(
							(currentLoadPolicy == LoadPolicy.PreserveChanges && c.EntityState == EntityState.Unchanged) ||
							(currentLoadPolicy == LoadPolicy.DiscardChanges && c.EntityState != EntityState.Unchanged)
						)
					))
				{
					c.SuppressEntityEvents = true;
					c.MasterUsername = (System.String)reader[((int)MastersColumn.MasterUsername - 1)];
					c.OriginalMasterUsername = c.MasterUsername;
					c.Password = (System.String)reader[((int)MastersColumn.Password - 1)];
					c.Identification = (reader.IsDBNull(((int)MastersColumn.Identification - 1)))?null:(System.String)reader[((int)MastersColumn.Identification - 1)];
					c.Name = (System.String)reader[((int)MastersColumn.Name - 1)];
					c.FirstName = (System.String)reader[((int)MastersColumn.FirstName - 1)];
					c.LastName = (System.String)reader[((int)MastersColumn.LastName - 1)];
					c.CountryId = (System.Int32)reader[((int)MastersColumn.CountryId - 1)];
					c.StateId = (System.Int32)reader[((int)MastersColumn.StateId - 1)];
					c.CityId = (System.Int32)reader[((int)MastersColumn.CityId - 1)];
					c.Email = (System.String)reader[((int)MastersColumn.Email - 1)];
					c.Mobile = (reader.IsDBNull(((int)MastersColumn.Mobile - 1)))?null:(System.String)reader[((int)MastersColumn.Mobile - 1)];
					c.Phone = (reader.IsDBNull(((int)MastersColumn.Phone - 1)))?null:(System.String)reader[((int)MastersColumn.Phone - 1)];
					c.IsActive = (System.Boolean)reader[((int)MastersColumn.IsActive - 1)];
					c.CreationDate = (System.DateTime)reader[((int)MastersColumn.CreationDate - 1)];
					c.EntityTrackingKey = key;
					c.AcceptChanges();
					c.SuppressEntityEvents = false;
				}
				rows.Add(c);
			}
		return rows;
		}		
		/// <summary>
		/// Refreshes the <see cref="GPSTrackingDBV2.Entities.Masters"/> object from the <see cref="IDataReader"/>.
		/// </summary>
		/// <param name="reader">The <see cref="IDataReader"/> to read from.</param>
		/// <param name="entity">The <see cref="GPSTrackingDBV2.Entities.Masters"/> object to refresh.</param>
		public static void RefreshEntity(IDataReader reader, GPSTrackingDBV2.Entities.Masters entity)
		{
			if (!reader.Read()) return;
			
			entity.MasterUsername = (System.String)reader[((int)MastersColumn.MasterUsername - 1)];
			entity.OriginalMasterUsername = (System.String)reader["MasterUsername"];
			entity.Password = (System.String)reader[((int)MastersColumn.Password - 1)];
			entity.Identification = (reader.IsDBNull(((int)MastersColumn.Identification - 1)))?null:(System.String)reader[((int)MastersColumn.Identification - 1)];
			entity.Name = (System.String)reader[((int)MastersColumn.Name - 1)];
			entity.FirstName = (System.String)reader[((int)MastersColumn.FirstName - 1)];
			entity.LastName = (System.String)reader[((int)MastersColumn.LastName - 1)];
			entity.CountryId = (System.Int32)reader[((int)MastersColumn.CountryId - 1)];
			entity.StateId = (System.Int32)reader[((int)MastersColumn.StateId - 1)];
			entity.CityId = (System.Int32)reader[((int)MastersColumn.CityId - 1)];
			entity.Email = (System.String)reader[((int)MastersColumn.Email - 1)];
			entity.Mobile = (reader.IsDBNull(((int)MastersColumn.Mobile - 1)))?null:(System.String)reader[((int)MastersColumn.Mobile - 1)];
			entity.Phone = (reader.IsDBNull(((int)MastersColumn.Phone - 1)))?null:(System.String)reader[((int)MastersColumn.Phone - 1)];
			entity.IsActive = (System.Boolean)reader[((int)MastersColumn.IsActive - 1)];
			entity.CreationDate = (System.DateTime)reader[((int)MastersColumn.CreationDate - 1)];
			entity.AcceptChanges();
		}
		
		/// <summary>
		/// Refreshes the <see cref="GPSTrackingDBV2.Entities.Masters"/> object from the <see cref="DataSet"/>.
		/// </summary>
		/// <param name="dataSet">The <see cref="DataSet"/> to read from.</param>
		/// <param name="entity">The <see cref="GPSTrackingDBV2.Entities.Masters"/> object.</param>
		public static void RefreshEntity(DataSet dataSet, GPSTrackingDBV2.Entities.Masters entity)
		{
			DataRow dataRow = dataSet.Tables[0].Rows[0];
			
			entity.MasterUsername = (System.String)dataRow["MasterUsername"];
			entity.OriginalMasterUsername = (System.String)dataRow["MasterUsername"];
			entity.Password = (System.String)dataRow["Password"];
			entity.Identification = Convert.IsDBNull(dataRow["Identification"]) ? null : (System.String)dataRow["Identification"];
			entity.Name = (System.String)dataRow["Name"];
			entity.FirstName = (System.String)dataRow["FirstName"];
			entity.LastName = (System.String)dataRow["LastName"];
			entity.CountryId = (System.Int32)dataRow["CountryID"];
			entity.StateId = (System.Int32)dataRow["StateID"];
			entity.CityId = (System.Int32)dataRow["CityID"];
			entity.Email = (System.String)dataRow["Email"];
			entity.Mobile = Convert.IsDBNull(dataRow["Mobile"]) ? null : (System.String)dataRow["Mobile"];
			entity.Phone = Convert.IsDBNull(dataRow["Phone"]) ? null : (System.String)dataRow["Phone"];
			entity.IsActive = (System.Boolean)dataRow["IsActive"];
			entity.CreationDate = (System.DateTime)dataRow["CreationDate"];
			entity.AcceptChanges();
		}
		#endregion 
		
		#region DeepLoad Methods
		/// <summary>
		/// Deep Loads the <see cref="IEntity"/> object with criteria based of the child 
		/// property collections only N Levels Deep based on the <see cref="DeepLoadType"/>.
		/// </summary>
		/// <remarks>
		/// Use this method with caution as it is possible to DeepLoad with Recursion and traverse an entire object graph.
		/// </remarks>
		/// <param name="transactionManager"><see cref="TransactionManager"/> object</param>
		/// <param name="entity">The <see cref="GPSTrackingDBV2.Entities.Masters"/> object to load.</param>
		/// <param name="deep">Boolean. A flag that indicates whether to recursively save all Property Collection that are descendants of this instance. If True, saves the complete object graph below this object. If False, saves this object only. </param>
		/// <param name="deepLoadType">DeepLoadType Enumeration to Include/Exclude object property collections from Load.</param>
		/// <param name="childTypes">GPSTrackingDBV2.Entities.Masters Property Collection Type Array To Include or Exclude from Load</param>
		/// <param name="innerList">A collection of child types for easy access.</param>
	    /// <exception cref="ArgumentNullException">entity or childTypes is null.</exception>
	    /// <exception cref="ArgumentException">deepLoadType has invalid value.</exception>
		public override void DeepLoad(TransactionManager transactionManager, GPSTrackingDBV2.Entities.Masters entity, bool deep, DeepLoadType deepLoadType, System.Type[] childTypes, DeepSession innerList)
		{
			if(entity == null)
				return;
			
			//used to hold DeepLoad method delegates and fire after all the local children have been loaded.
			Dictionary<string, KeyValuePair<Delegate, object>> deepHandles = new Dictionary<string, KeyValuePair<Delegate, object>>();
			// Deep load child collections  - Call GetByMasterUsername methods when available
			
			#region CompaniesByMastersCollection
			//Relationship Type One : Many
			if (CanDeepLoad(entity, "List<CompaniesByMasters>|CompaniesByMastersCollection", deepLoadType, innerList)) 
			{
				#if NETTIERS_DEBUG
				System.Diagnostics.Debug.WriteLine("- property 'CompaniesByMastersCollection' loaded. key " + entity.EntityTrackingKey);
				#endif 

				entity.CompaniesByMastersCollection = DataRepository.CompaniesByMastersProvider.GetByMasterUsername(transactionManager, entity.MasterUsername);

				if (deep && entity.CompaniesByMastersCollection.Count > 0)
				{
					deepHandles.Add("CompaniesByMastersCollection",
						new KeyValuePair<Delegate, object>((DeepLoadHandle<CompaniesByMasters>) DataRepository.CompaniesByMastersProvider.DeepLoad,
						new object[] { transactionManager, entity.CompaniesByMastersCollection, deep, deepLoadType, childTypes, innerList }
					));
				}
			}		
			#endregion 
			
			
			#region CompanyIdCompaniesCollection_From_CompaniesByMasters
			// RelationshipType.ManyToMany
			if (CanDeepLoad(entity, "List<Companies>|CompanyIdCompaniesCollection_From_CompaniesByMasters", deepLoadType, innerList))
			{
				entity.CompanyIdCompaniesCollection_From_CompaniesByMasters = DataRepository.CompaniesProvider.GetByMasterUsernameFromCompaniesByMasters(transactionManager, entity.MasterUsername);			 
		
				#if NETTIERS_DEBUG
				System.Diagnostics.Debug.WriteLine("- property 'CompanyIdCompaniesCollection_From_CompaniesByMasters' loaded. key " + entity.EntityTrackingKey);
				#endif 
				
				if (deep && entity.CompanyIdCompaniesCollection_From_CompaniesByMasters != null)
				{
					deepHandles.Add("CompanyIdCompaniesCollection_From_CompaniesByMasters",
						new KeyValuePair<Delegate, object>((DeepLoadHandle< Companies >) DataRepository.CompaniesProvider.DeepLoad,
						new object[] { transactionManager, entity.CompanyIdCompaniesCollection_From_CompaniesByMasters, deep, deepLoadType, childTypes, innerList }
					));
				}
			}
			#endregion
			
			
			
			#region MasterRoleIdMasterRolesCollection_From_MastersRolesByMaster
			// RelationshipType.ManyToMany
			if (CanDeepLoad(entity, "List<MasterRoles>|MasterRoleIdMasterRolesCollection_From_MastersRolesByMaster", deepLoadType, innerList))
			{
				entity.MasterRoleIdMasterRolesCollection_From_MastersRolesByMaster = DataRepository.MasterRolesProvider.GetByMasterUsernameFromMastersRolesByMaster(transactionManager, entity.MasterUsername);			 
		
				#if NETTIERS_DEBUG
				System.Diagnostics.Debug.WriteLine("- property 'MasterRoleIdMasterRolesCollection_From_MastersRolesByMaster' loaded. key " + entity.EntityTrackingKey);
				#endif 
				
				if (deep && entity.MasterRoleIdMasterRolesCollection_From_MastersRolesByMaster != null)
				{
					deepHandles.Add("MasterRoleIdMasterRolesCollection_From_MastersRolesByMaster",
						new KeyValuePair<Delegate, object>((DeepLoadHandle< MasterRoles >) DataRepository.MasterRolesProvider.DeepLoad,
						new object[] { transactionManager, entity.MasterRoleIdMasterRolesCollection_From_MastersRolesByMaster, deep, deepLoadType, childTypes, innerList }
					));
				}
			}
			#endregion
			
			
			
			#region MastersRolesByMasterCollection
			//Relationship Type One : Many
			if (CanDeepLoad(entity, "List<MastersRolesByMaster>|MastersRolesByMasterCollection", deepLoadType, innerList)) 
			{
				#if NETTIERS_DEBUG
				System.Diagnostics.Debug.WriteLine("- property 'MastersRolesByMasterCollection' loaded. key " + entity.EntityTrackingKey);
				#endif 

				entity.MastersRolesByMasterCollection = DataRepository.MastersRolesByMasterProvider.GetByMasterUsername(transactionManager, entity.MasterUsername);

				if (deep && entity.MastersRolesByMasterCollection.Count > 0)
				{
					deepHandles.Add("MastersRolesByMasterCollection",
						new KeyValuePair<Delegate, object>((DeepLoadHandle<MastersRolesByMaster>) DataRepository.MastersRolesByMasterProvider.DeepLoad,
						new object[] { transactionManager, entity.MastersRolesByMasterCollection, deep, deepLoadType, childTypes, innerList }
					));
				}
			}		
			#endregion 
			
			
			//Fire all DeepLoad Items
			foreach(KeyValuePair<Delegate, object> pair in deepHandles.Values)
		    {
                pair.Key.DynamicInvoke((object[])pair.Value);
		    }
			deepHandles = null;
		}
		
		#endregion 
		
		#region DeepSave Methods

		/// <summary>
		/// Deep Save the entire object graph of the GPSTrackingDBV2.Entities.Masters object with criteria based of the child 
		/// Type property array and DeepSaveType.
		/// </summary>
		/// <param name="transactionManager">The transaction manager.</param>
		/// <param name="entity">GPSTrackingDBV2.Entities.Masters instance</param>
		/// <param name="deepSaveType">DeepSaveType Enumeration to Include/Exclude object property collections from Save.</param>
		/// <param name="childTypes">GPSTrackingDBV2.Entities.Masters Property Collection Type Array To Include or Exclude from Save</param>
		/// <param name="innerList">A Hashtable of child types for easy access.</param>
		public override bool DeepSave(TransactionManager transactionManager, GPSTrackingDBV2.Entities.Masters entity, DeepSaveType deepSaveType, System.Type[] childTypes, DeepSession innerList)
		{	
			if (entity == null)
				return false;
							
			#region Composite Parent Properties
			//Save Source Composite Properties, however, don't call deep save on them.  
			//So they only get saved a single level deep.
			#endregion Composite Parent Properties

			// Save Root Entity through Provider
			if (!entity.IsDeleted)
				this.Save(transactionManager, entity);
			
			//used to hold DeepSave method delegates and fire after all the local children have been saved.
			Dictionary<string, KeyValuePair<Delegate, object>> deepHandles = new Dictionary<string, KeyValuePair<Delegate, object>>();

			#region CompanyIdCompaniesCollection_From_CompaniesByMasters>
			if (CanDeepSave(entity.CompanyIdCompaniesCollection_From_CompaniesByMasters, "List<Companies>|CompanyIdCompaniesCollection_From_CompaniesByMasters", deepSaveType, innerList))
			{
				if (entity.CompanyIdCompaniesCollection_From_CompaniesByMasters.Count > 0 || entity.CompanyIdCompaniesCollection_From_CompaniesByMasters.DeletedItems.Count > 0)
				{
					DataRepository.CompaniesProvider.Save(transactionManager, entity.CompanyIdCompaniesCollection_From_CompaniesByMasters); 
					deepHandles.Add("CompanyIdCompaniesCollection_From_CompaniesByMasters",
						new KeyValuePair<Delegate, object>((DeepSaveHandle<Companies>) DataRepository.CompaniesProvider.DeepSave,
						new object[] { transactionManager, entity.CompanyIdCompaniesCollection_From_CompaniesByMasters, deepSaveType, childTypes, innerList }
					));
				}
			}
			#endregion 

			#region MasterRoleIdMasterRolesCollection_From_MastersRolesByMaster>
			if (CanDeepSave(entity.MasterRoleIdMasterRolesCollection_From_MastersRolesByMaster, "List<MasterRoles>|MasterRoleIdMasterRolesCollection_From_MastersRolesByMaster", deepSaveType, innerList))
			{
				if (entity.MasterRoleIdMasterRolesCollection_From_MastersRolesByMaster.Count > 0 || entity.MasterRoleIdMasterRolesCollection_From_MastersRolesByMaster.DeletedItems.Count > 0)
				{
					DataRepository.MasterRolesProvider.Save(transactionManager, entity.MasterRoleIdMasterRolesCollection_From_MastersRolesByMaster); 
					deepHandles.Add("MasterRoleIdMasterRolesCollection_From_MastersRolesByMaster",
						new KeyValuePair<Delegate, object>((DeepSaveHandle<MasterRoles>) DataRepository.MasterRolesProvider.DeepSave,
						new object[] { transactionManager, entity.MasterRoleIdMasterRolesCollection_From_MastersRolesByMaster, deepSaveType, childTypes, innerList }
					));
				}
			}
			#endregion 
	
			#region List<CompaniesByMasters>
				if (CanDeepSave(entity.CompaniesByMastersCollection, "List<CompaniesByMasters>|CompaniesByMastersCollection", deepSaveType, innerList)) 
				{	
					// update each child parent id with the real parent id (mostly used on insert)
					foreach(CompaniesByMasters child in entity.CompaniesByMastersCollection)
					{
						if(child.MasterUsernameSource != null)
						{
								child.MasterUsername = child.MasterUsernameSource.MasterUsername;
						}

						if(child.CompanyIdSource != null)
						{
								child.CompanyId = child.CompanyIdSource.CompanyId;
						}

					}

					if (entity.CompaniesByMastersCollection.Count > 0 || entity.CompaniesByMastersCollection.DeletedItems.Count > 0)
					{
						//DataRepository.CompaniesByMastersProvider.Save(transactionManager, entity.CompaniesByMastersCollection);
						
						deepHandles.Add("CompaniesByMastersCollection",
						new KeyValuePair<Delegate, object>((DeepSaveHandle< CompaniesByMasters >) DataRepository.CompaniesByMastersProvider.DeepSave,
							new object[] { transactionManager, entity.CompaniesByMastersCollection, deepSaveType, childTypes, innerList }
						));
					}
				} 
			#endregion 
				
	
			#region List<MastersRolesByMaster>
				if (CanDeepSave(entity.MastersRolesByMasterCollection, "List<MastersRolesByMaster>|MastersRolesByMasterCollection", deepSaveType, innerList)) 
				{	
					// update each child parent id with the real parent id (mostly used on insert)
					foreach(MastersRolesByMaster child in entity.MastersRolesByMasterCollection)
					{
						if(child.MasterUsernameSource != null)
						{
								child.MasterUsername = child.MasterUsernameSource.MasterUsername;
						}

						if(child.MasterRoleIdSource != null)
						{
								child.MasterRoleId = child.MasterRoleIdSource.MasterRoleId;
						}

					}

					if (entity.MastersRolesByMasterCollection.Count > 0 || entity.MastersRolesByMasterCollection.DeletedItems.Count > 0)
					{
						//DataRepository.MastersRolesByMasterProvider.Save(transactionManager, entity.MastersRolesByMasterCollection);
						
						deepHandles.Add("MastersRolesByMasterCollection",
						new KeyValuePair<Delegate, object>((DeepSaveHandle< MastersRolesByMaster >) DataRepository.MastersRolesByMasterProvider.DeepSave,
							new object[] { transactionManager, entity.MastersRolesByMasterCollection, deepSaveType, childTypes, innerList }
						));
					}
				} 
			#endregion 
				
			//Fire all DeepSave Items
			foreach(KeyValuePair<Delegate, object> pair in deepHandles.Values)
		    {
                pair.Key.DynamicInvoke((object[])pair.Value);
		    }
			
			// Save Root Entity through Provider, if not already saved in delete mode
			if (entity.IsDeleted)
				this.Save(transactionManager, entity);
				

			deepHandles = null;
						
			return true;
		}
		#endregion
	} // end class
	
	#region MastersChildEntityTypes
	
	///<summary>
	/// Enumeration used to expose the different child entity types 
	/// for child properties in <c>GPSTrackingDBV2.Entities.Masters</c>
	///</summary>
	public enum MastersChildEntityTypes
	{

		///<summary>
		/// Collection of <c>Masters</c> as OneToMany for CompaniesByMastersCollection
		///</summary>
		[ChildEntityType(typeof(TList<CompaniesByMasters>))]
		CompaniesByMastersCollection,

		///<summary>
		/// Collection of <c>Masters</c> as ManyToMany for CompaniesCollection_From_CompaniesByMasters
		///</summary>
		[ChildEntityType(typeof(TList<Companies>))]
		CompanyIdCompaniesCollection_From_CompaniesByMasters,

		///<summary>
		/// Collection of <c>Masters</c> as ManyToMany for MasterRolesCollection_From_MastersRolesByMaster
		///</summary>
		[ChildEntityType(typeof(TList<MasterRoles>))]
		MasterRoleIdMasterRolesCollection_From_MastersRolesByMaster,

		///<summary>
		/// Collection of <c>Masters</c> as OneToMany for MastersRolesByMasterCollection
		///</summary>
		[ChildEntityType(typeof(TList<MastersRolesByMaster>))]
		MastersRolesByMasterCollection,
	}
	
	#endregion MastersChildEntityTypes
	
	#region MastersFilterBuilder
	
	/// <summary>
	/// A strongly-typed instance of the <see cref="SqlFilterBuilder&lt;MastersColumn&gt;"/> class
	/// that is used exclusively with a <see cref="Masters"/> object.
	/// </summary>
	[CLSCompliant(true)]
	public class MastersFilterBuilder : SqlFilterBuilder<MastersColumn>
	{
		#region Constructors

		/// <summary>
		/// Initializes a new instance of the MastersFilterBuilder class.
		/// </summary>
		public MastersFilterBuilder() : base() { }

		/// <summary>
		/// Initializes a new instance of the MastersFilterBuilder class.
		/// </summary>
		/// <param name="ignoreCase">Specifies whether to create case-insensitive statements.</param>
		public MastersFilterBuilder(bool ignoreCase) : base(ignoreCase) { }

		/// <summary>
		/// Initializes a new instance of the MastersFilterBuilder class.
		/// </summary>
		/// <param name="ignoreCase">Specifies whether to create case-insensitive statements.</param>
		/// <param name="useAnd">Specifies whether to combine statements using AND or OR.</param>
		public MastersFilterBuilder(bool ignoreCase, bool useAnd) : base(ignoreCase, useAnd) { }

		#endregion Constructors
	}

	#endregion MastersFilterBuilder
	
	#region MastersParameterBuilder
	
	/// <summary>
	/// A strongly-typed instance of the <see cref="ParameterizedSqlFilterBuilder&lt;MastersColumn&gt;"/> class
	/// that is used exclusively with a <see cref="Masters"/> object.
	/// </summary>
	[CLSCompliant(true)]
	public class MastersParameterBuilder : ParameterizedSqlFilterBuilder<MastersColumn>
	{
		#region Constructors

		/// <summary>
		/// Initializes a new instance of the MastersParameterBuilder class.
		/// </summary>
		public MastersParameterBuilder() : base() { }

		/// <summary>
		/// Initializes a new instance of the MastersParameterBuilder class.
		/// </summary>
		/// <param name="ignoreCase">Specifies whether to create case-insensitive statements.</param>
		public MastersParameterBuilder(bool ignoreCase) : base(ignoreCase) { }

		/// <summary>
		/// Initializes a new instance of the MastersParameterBuilder class.
		/// </summary>
		/// <param name="ignoreCase">Specifies whether to create case-insensitive statements.</param>
		/// <param name="useAnd">Specifies whether to combine statements using AND or OR.</param>
		public MastersParameterBuilder(bool ignoreCase, bool useAnd) : base(ignoreCase, useAnd) { }

		#endregion Constructors
	}

	#endregion MastersParameterBuilder
	
	#region MastersSortBuilder
    
    /// <summary>
    /// A strongly-typed instance of the <see cref="SqlSortBuilder&lt;MastersColumn&gt;"/> class
	/// that is used exclusively with a <see cref="Masters"/> object.
    /// </summary>
    [CLSCompliant(true)]
    public class MastersSortBuilder : SqlSortBuilder<MastersColumn>
    {
		#region Constructors

		/// <summary>
		/// Initializes a new instance of the MastersSqlSortBuilder class.
		/// </summary>
		public MastersSortBuilder() : base() { }

		#endregion Constructors

    }    
    #endregion MastersSortBuilder
	
} // end namespace
